package com.ete.service.dialog.log

import android.content.Context
import android.view.LayoutInflater
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableList
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.ToastUtils
import com.ete.service.callback.ICallback
import com.ete.service.dialog.BaseDialog
import com.ete.service.entry.log.AppDirEntry
import com.ete.service.entry.log.AppLogEntry
import com.ete.service.service.R
import com.ete.service.service.BR
import com.ete.service.service.databinding.DialogAppDirInfoBinding
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * Author:LiXian
 * <p>
 * Desc:选择上报日志目录
 * Version:1.0
 * date 2024/4/25 18:36
 */
class ChooseUploadDirDialog(
        mContext: Context,
        private var mZipEntry: AppLogEntry,
        private var mCallback: (MutableList<String>, String) -> Unit
) : BaseDialog(mContext, R.style.XUIDialog_Custom) {

    val items: ObservableList<AppDirEntry> = ObservableArrayList()
    val binding: ItemBinding<AppDirEntry> = ItemBinding.of<AppDirEntry>(BR.entry, R.layout.item_app_dir)
        .bindExtra(BR.callback, object : ICallback<AppDirEntry> {
            override fun callback(t: AppDirEntry) {
                t.isChecked.set(!t.isChecked.get())
            }
        })

    private val mBinding = DialogAppDirInfoBinding.inflate(LayoutInflater.from(context))

    fun initDialog(mInputContent: String) {
        initItems()
        mBinding.chooseDialog = this
        mBinding.dialogIvClose.setOnClickListener { dismiss() }
        mBinding.dialogBtnCancel.setOnClickListener { dismiss() }
        mBinding.dialogBtnOk.updateThemeColor(mZipEntry.mAppThemeColor)
        mBinding.dialogEtInput.setText(mInputContent)
        mBinding.dialogBtnOk.setOnClickListener {
            val mDescription = mBinding.dialogEtInput.text?.toString() ?: ""
            if (mDescription.isEmpty()) {
                ToastUtils.showShort(R.string.upload_input_des_question)
            } else {
                val chooseDirs = items.filter { it.isChecked.get() }.map { it.dirPath }.toMutableList()
                chooseDirs.addAll(0, mZipEntry.mDefaultUploadDirs)
                mCallback.invoke(chooseDirs, mDescription)
                dismiss()
            }
        }
        setContentView(mBinding.root)
    }

    private fun initItems() {
        items.clear()
        val appDirEntries: List<AppDirEntry> = if (mZipEntry.mAppHostPath.isEmpty()) {
            mZipEntry.mAppDirs.map {
                AppDirEntry(
                        FileUtils.getFileNameNoExtension(it),
                        it, ObservableBoolean(true)
                )
            }
        } else {
            val mFiles = FileUtils.listFilesInDir(mZipEntry.mAppHostPath)
            mFiles?.filter {
                //目录上传的列表，过滤掉。上传时带上
                !mZipEntry.mDefaultUploadDirs.contains(it.absolutePath)
            }?.map {
                val isChecked = mZipEntry.mAppDirs.contains(it.absolutePath)
                AppDirEntry(
                        FileUtils.getFileNameNoExtension(it),
                        it.absolutePath,
                        ObservableBoolean(isChecked)
                )
            } ?: mutableListOf()
        }
        items.addAll(appDirEntries)
    }

    override fun getDialogWidthRate(): Float = 0.55F
}